package test.my.com.myapplicationtest.retrofit_net;

import android.text.TextUtils;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import test.my.com.myapplicationtest.MyApplication;
import test.my.com.myapplicationtest.utils.LogUtil;

/**
 * Author : Cooper
 * Time : 2017/8/23  10:45
 * Description : 请求参数拦截器
 *               1.缓存：主要是通过http协议里面的control-cache控制缓存，而且是仅仅只能是Get请求才能缓存，如果Post请求OkHttp会让response返回null，同时报504错误，也就是没缓存。
 */

public class RequestBasicParamsInterceptor implements Interceptor {
    public static final MediaType MEDIA_TYPE
            = MediaType.parse("application/json; charset=UTF-8");
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        Request.Builder builder = request.newBuilder();

//        String headInfo = DeviceUtil.getHeadInfo(MyApplication.getApplication());
//        builder.addHeader("key1","one")
//                .addHeader("key2","two")
//                .addHeader("User-Agent",headInfo).build();
//        String lastSessionId = SharedPreferenceUtil.getSessionId(MyApplication.getApplication());
//        LogUtil.e("jsonrpc------session_id---->" + lastSessionId);
//        if (!TextUtils.isEmpty(lastSessionId)) {
//            builder.addHeader("Cookie", lastSessionId);
//        }
//
//        if (!ConnectionUtil.isConnected(MyApplication.getApplication())) {
//            builder.cacheControl(CacheControl.FORCE_CACHE);
//        }
//        RequestEntity requestEntity = new RequestEntity();
//        requestEntity.setId(UUID.randomUUID().hashCode());
//        requestEntity.setJsonrpc("2.0");
//        requestEntity.setMethod("marginInfo");
//        List<Object> list = new ArrayList<>();
//        requestEntity.setParams(list);
//        String requestStr = GsonUtil.gsonString(requestEntity);
//
//        builder.post(RequestBody.create(MEDIA_TYPE,requestStr));
        request = builder.build();




        Response response = chain.proceed(request);
//        if (ConnectionUtil.isConnected(ZhuanBeiApplication.getContext())) {
//            int maxAge = 0 * 60;
//            // 有网络时 设置缓存超时时间0个小时
//            response.newBuilder()
//                    .removeHeader("Pragma")// 清除头信息，因为服务器如果不支持，会返回一些干扰信息，不清除下面无法生效
//                    .header("Cache-Control", "public, max-age=" + maxAge)
//                    .build();
//        } else {
//            // 无网络时，设置超时为4周
//            int maxStale = 60 * 60 * 24 * 28;
//            response.newBuilder()
//                    .removeHeader("Pragma")
//                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
//                    .build();
//        }
        String responseHeader = response.header("Set-Cookie");
//        String responseStr = new String(response.body().bytes());

//        LogUtil.e("responseHeader  == "+responseHeader);
//        LogUtil.e("responseStr  == "+responseStr);
//        String responseHeader = response.header("Set-Cookie");
//        LogUtil.e("responseHeader  == "+responseHeader);
        return response;
    }


}
